﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct stu2
{
	//1.成员列表；
	//char name[21];
	//float scores[3];
	char* name;
	float* scores;
};

//main125：堆内存开辟结构体空间实现学生成绩按升序排名
int main125()
{
	struct stu2* p = (struct stu2*)malloc(sizeof(struct stu2) * 3);
	for (int i = 0; i < 3; i++)
	{
		p[i].name = (char*)malloc(sizeof(char) * 21);
		p[i].scores = (float*)malloc(sizeof(float) * 3);
		printf("1.请您输入，学生姓名，成绩：\n");
		scanf("%s%f%f%f", p[i].name, &p[i].scores[0], &p[i].scores[1], &p[i].scores[2]);
		//张三 98 87 90			李四 89 78 79			王五 69 78 80

	}

	//4.冒泡排序；
	for (int i = 0; i < 3 - 1; i++)
	{
		for (int j = 0; j < 3 - i - 1; j++)
		{
			float sum1 = p[j].scores[0] + p[j].scores[1] + p[j].scores[2];
			float sum2 = p[j + 1].scores[0] + p[j + 1].scores[1] + p[j + 1].scores[2];
			if (sum1 > sum2)
			{
				struct stu2 temp = p[j];
				p[j] = p[j + 1];
				p[j + 1] = temp;
			}
		}
	}

	for (int i = 0; i < 3; i++)
	{
		printf("姓名：%s\n", p[i].name);//姓名：尼古拉斯

		//3.%.1f是保留一位小数输出的意思；
		printf("成绩：%.1f	%.1f	%.1f\n", p[i].scores[0], p[i].scores[1], p[i].scores[2]);//成绩：100.0     200.0   300.0
	}

	/*姓名：王五
	成绩：69.0      78.0    80.0
	姓名：李四
	成绩：89.0      78.0    79.0
	姓名：张三
	成绩：98.0      87.0    90.0*/

	//2.释放堆内存；
	for (int i = 0; i < 3; i++)
	{
		free(p[i].name);
		free(p[i].scores);
	}
	free(p);

	system("pause");
	return EXIT_SUCCESS;
}

//main126：指针修改栈内存字符串
int main126(void)
{
	/*char* p1 = "hello";
	char* p2 = "world";*/
	char arr1[] = "hello";
	char arr2[] = "world";
	char* p1 = arr1;
	char* p2 = arr2;

	char* temp = p1;
	p1 = p2;
	p2 = temp;
	printf("%s\n", p1);//world
	printf("%s\n", p2);//hello

	printf("%c\n", p1[0]);//w
	printf("%c\n", p2[0]);//h

	printf("%c\n", *p1 + 1);//w-->w+1=x
	printf("%c\n", *(p2 + 1));//h-->e

	//5.语法上没问题，只是这里的的char* p1="world"；p1指向的是字符串常量池，里面的值不能被修改，
		//不能出现horld，故下面的报错，不成立。p2同理。现在我改了，改成就是指针p1和p2指向栈区字符串了；
	char tempChar = p1[0];
	p1[0] = p2[0];
	p2[0] = tempChar;

	printf("%s\n", p1);//horld
	printf("%s\n", p2);//wello
	return 0;
}